package piccolo;

import java.awt.Point;
import java.awt.geom.Point2D;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import edu.umd.cs.piccolo.PCanvas;

public class ZoomSlider extends JSlider implements ChangeListener {
	private static final long serialVersionUID = -1245370183643555029L;
	private static ZoomSlider instance;
	
	public static final int ZOOM_MIN = 1;
	public static final int ZOOM_MAX = 200;
	public static final int ZOOM_TICK = 5;
	
	public static ZoomSlider getInstance()
	{
		if(instance==null) instance = new ZoomSlider();
		return instance;
	}
	
	public ZoomSlider()
	{
		super();
		this.setMaximum(ZOOM_MAX);
		this.setMinimum(ZOOM_MIN);
		this.setMajorTickSpacing(ZOOM_TICK);
		this.setDoubleBuffered(true);
		this.setMinorTickSpacing(1);
		this.setOrientation(JSlider.HORIZONTAL);
		this.setPaintTicks(true);
		this.setPaintTrack(true);
		this.setSnapToTicks(true);
		this.setValue(ZOOM_MAX/2);
		this.setVisible(true);
		this.addChangeListener(this);
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		PCanvas canvas = P2DCanvas.getInstance();
		JSlider slider = (JSlider) e.getSource();
		double currentScale = canvas.getCamera().getScale();
        double scaleDelta = (double)slider.getValue()/100;
        System.out.println("SLIDER: " + slider.getValue());
        System.out.println("CURRENT SCALE: " + currentScale);
        System.out.println("SCALEDELTA: " + scaleDelta);
        double newScale = scaleDelta;
        System.out.println("NEWSCALE: " + newScale);
        
        int x = (int) canvas.getBounds().getCenterX();
        int y = (int) canvas.getBounds().getCenterY();
        Point2D pos = new Point(x, y);
        
        canvas.getCamera().setScale(scaleDelta);
        canvas.getCamera().scaleViewAboutPoint(scaleDelta, pos.getX(),pos.getY()); 
	}
	
	
}
